package cn.mcsys.viewer.core.utils;

import cn.mcsys.myadmin.common.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author likai
 * @Date 2018/09/06
 */
public class ParseTradeFile {
    private static Logger logger = LoggerFactory.getLogger(ParseTradeFile.class);

    public static void main(String[] args) {
       String path = "D:\\air_show\\viewer\\src\\main\\java\\cn\\mcsys\\viewer\\core\\utils\\x.bin";
       ParseTradeFile parseTradeFile = new ParseTradeFile();
       parseTradeFile.parseFile(path);
    }

    public void parseFile(String path){
        FileInputStream fileInputStream;
        try {
            fileInputStream = new FileInputStream(path);

            TradeHeader tradeHeader = new TradeHeader();
            tradeHeader.setFile(fileInputStream);
            tradeHeader.parse();
            for (int i = 0; i < tradeHeader.getRecordNumber(); i++) {
                /**
                 * 读取交易类型
                 * 0x3C    航展M1卡
                 * 0x3D    航展二维码
                 */
                int tradeType = fileInputStream.read();
                if (tradeType == 0x3c) {
                    M1TradeBody m1TradeBody = new M1TradeBody(fileInputStream);
                } else if (tradeType == 0x3d) {
                    QRCodeTradeBody qrCodeTradeBody = new QRCodeTradeBody(fileInputStream);
                } else {
                    logger.debug("未知的交易类型");
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
    }
}
